/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.telsoft.data;

import com.telsoft.business.DAOHelperBusiness;
import com.telsoft.entity.ChildrenEntity;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author thieplv
 */
public class ChildrenDAO {
    private Connection conn;
  public List<ChildrenEntity> getChildren() throws SQLException{
     List<ChildrenEntity> children = new ArrayList<ChildrenEntity>();
     conn = DAOHelperBusiness.getConnection();
     Statement stm = DAOHelperBusiness.getStatement(conn);
     String sqlChildren = "SELECT * FROM CHILDREN_COMPONENT";
     String sqlChildAttr = "SELECT * FROM CHILD_ATT";
     ResultSet rsChildren = stm.executeQuery(sqlChildren);
     ResultSet rsChildAttr = stm.executeQuery(sqlChildAttr);
     while(rsChildren.next()){
         ChildrenEntity child = new ChildrenEntity();
         List<Integer> attrIDs = new ArrayList<Integer>();
         while(rsChildAttr.next()){
             if(rsChildren.getInt("CHILD_ID") == rsChildAttr.getInt("CHILD_ID")){
              attrIDs.add(rsChildAttr.getInt("ATT_ID"));   
             }
         }
        child.setAttID(attrIDs);
        child.setChildID(rsChildren.getInt("CHILD_ID"));
        child.setCompID(rsChildren.getInt("COM_ID"));
        child.setDetail(rsChildren.getString("DETAIL"));
     }
     DAOHelperBusiness.closeConnection(conn);
     DAOHelperBusiness.closeStatement(stm);
     DAOHelperBusiness.closeResultSet(rsChildren);
     DAOHelperBusiness.closeResultSet(rsChildAttr);   
     return children;
}
}
